﻿using DTcms.Common;
using System;
using System.Data;
using System.Web.Mvc;

namespace DTcms.Web.Mvc.UI
{
    public partial class BaseController : Controller
    {
        /// <summary>
        /// 获取tag名
        /// </summary>
        /// <returns>DataTable</returns>
        public string get_article_tags_title(int id)
        {
            Model.article_tags model = new BLL.article_tags().Get(id);
            if (model == null)
            {
                return "无标签";
            }
            else
            {
                return model.title;
            }
        }

        /// <summary>
        /// 文章Tags标签列表
        /// </summary>
        /// <param name="top">显示条数</param>
        /// <param name="strwhere">查询条件</param>
        /// <returns>DataTable</returns>
        public DataTable get_article_tags(int top, string strwhere)
        {
            return new BLL.article_tags().GetList(top, strwhere, "count desc").Tables[0];
        }

        /// <summary>
        /// 文章Tags标签列表
        /// </summary>
        /// <param name="top">显示条数</param>
        /// <param name="strwhere">查询条件</param>
        /// <returns>DataTable</returns>
        public DataTable get_article_tags(int top, string strwhere, string filedOrder)
        {
            return new BLL.article_tags().GetList(top, strwhere, filedOrder).Tables[0];
        }

        /// <summary>
        /// 查询数据
        /// </summary>
        public DataTable get_tags_list(Model.sites site, int _tags_id, int _page, int _pagesize, out int _totalcount)
        {
            //创建一个DataTable
            DataTable dt = new DataTable();
            dt.Columns.Add("id", Type.GetType("System.Int32"));
            dt.Columns.Add("channel_id", Type.GetType("System.String"));
            dt.Columns.Add("click", Type.GetType("System.String"));
            dt.Columns.Add("title", Type.GetType("System.String"));
            dt.Columns.Add("remark", Type.GetType("System.String"));
            dt.Columns.Add("link_url", Type.GetType("System.String"));
            dt.Columns.Add("add_time", Type.GetType("System.String"));
            dt.Columns.Add("img_url", Type.GetType("System.String"));
            //创建一个DataSet,判断是使用Tags还是关健字查询
            DataSet ds = new DataSet();
            int _siteid = site.id;
            if (site.inherit_id > 0)
            {
                _siteid = site.inherit_id;
            }
            ds = new BLL.article().ArticleSearch(_siteid, _tags_id, _pagesize, _page, string.Empty, "add_time desc,id desc", out _totalcount);
            if (ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    DataRow dr1 = ds.Tables[0].Rows[i];
                    string link_url = get_url_rewrite(Utils.StrToInt(dr1["channel_id"].ToString(), 0), dr1["call_index"].ToString(), Utils.StrToInt(dr1["id"].ToString(), 0));
                    if (!string.IsNullOrEmpty(link_url))
                    {
                        DataRow dr = dt.NewRow();
                        dr["id"] = dr1["id"]; //自增ID
                        dr["click"] = dr1["click"]; //浏览数
                        dr["channel_id"] = dr1["channel_id"]; //频道ID
                        dr["title"] = dr1["title"]; //标题
                        dr["remark"] = dr1["zhaiyao"]; //摘要
                        dr["link_url"] = link_url; //链接地址
                        dr["add_time"] = dr1["add_time"]; //发布时间
                        dr["img_url"] = dr1["img_url"]; //发布时间
                        dt.Rows.Add(dr);
                    }
                }
            }
            return dt;
        }

        /// <summary>
        /// 获取文章Tags标签列表
        /// </summary>
        /// <param name="tags">标签列表</param>
        /// <returns></returns>
        public DataTable get_article_tags(string tags)
        {
            //创建一个DataTable
            DataTable dt = new DataTable();
            dt.Columns.Add("id", Type.GetType("System.Int32"));
            dt.Columns.Add("title", Type.GetType("System.String"));
            dt.Columns.Add("call_index", Type.GetType("System.String"));

            if (string.IsNullOrEmpty(tags) || "" == tags)
            {
                return dt;
            }
            tags = tags.Replace("，", ",");
            string[] arr = tags.Split(',');
            //遍历
            BLL.article_tags bll = new BLL.article_tags();
            foreach (string t in arr)
            {
                Model.article_tags model = bll.GetModel("title='" + t + "'", "top 1 *", "");
                if (null != model)
                {
                    DataRow dr = dt.NewRow();
                    dr["id"] = model.id;
                    dr["title"] = model.title;
                    dr["call_index"] = model.call_index;
                    dt.Rows.Add(dr);
                }
            }
            return dt;
        }
    }
}
